<?php require('connections/con_gl.php'); ?>
<?php
cekAkses($_SESSION[akses],"L-1");
?>
<link href="css/ui.datepicker.css" type="text/css" rel="stylesheet" />
<!--<script language="JavaScript" src="js/FusionCharts.js"></script>
<script type="text/javascript" src="calendar/jquery-1.2.3.js"></script>-->
<script type="text/javascript" src="js/ui.datepicker.js"></script>
<script type="text/javascript">
<!--
$(function()
      {
        $('.calendar').datepicker({
            appendText : "",
            dateFormat : 'yy/mm/dd'
          });
      });
</script>
<h1>RUGI LABA  PERUSAHAAN</h1> <?php if(isset($_POST[Search]) AND $_POST[Mulai]<>'Mulai...' AND $_POST[Sampai]<>'Sampai...') { ?><?php tanggal($_POST[Mulai],"tampilkan");?> - <?php tanggal($_POST[Sampai],"tampilkan");?><?php } ?>
<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0"> 
  <tr valign="top" class="hide">
    <td align="right"><form id="search" name="search" method="post" action="">
      <label>
        <input name="Mulai" type="text" class="calendar" id="Mulai" value="<?php if(empty($_POST[Mulai])) echo "Mulai...";else echo $_POST[Mulai];?>" onblur="if(this.value=='') this.value='Mulai...';" onfocus="if(this.value=='Mulai...') this.value='';"></label>
      s/d
      <label><input name="Sampai" type="text" class="calendar" id="Sampai" value="<?php if(empty($_POST[Sampai])) echo "Sampai...";else echo $_POST[Sampai];?>" onblur="if(this.value=='') this.value='Sampai...';" onfocus="if(this.value=='Sampai...') this.value='';"></label>
      <label>
        <input name="Search" type="submit" id="Search" value="Tampilkan" />
        </label>
    </form>    </td>
  </tr>
  <?php if(isset($_POST['Search'])) { ?>
  <?php
  mysql_select_db($database_con_gl, $con_gl);
$query_gethasil = "SELECT kd, klasifikasi FROM gl_klas WHERE ruglab = 1 ORDER BY gl_klas.kd, gl_klas.klasifikasi";
$gethasil = mysql_query($query_gethasil, $con_gl) or die(mysql_error());
$row_gethasil = mysql_fetch_assoc($gethasil);
$totalRows_gethasil = mysql_num_rows($gethasil);

mysql_select_db($database_con_gl, $con_gl);
$query_getbeban = "SELECT kd, tipe, klasifikasi FROM gl_klas WHERE ruglab = 2 ORDER BY gl_klas.kd, gl_klas.klasifikasi";
$getbeban = mysql_query($query_getbeban, $con_gl) or die(mysql_error());
$row_getbeban = mysql_fetch_assoc($getbeban);
$totalRows_getbeban = mysql_num_rows($getbeban);
  ?>
  <tr>
    <td ><table width="100%" border="0" cellspacing="0" cellpadding="5" class="datatable">
        <tr>
          <td width="25%" align="left"><strong>Pendapatan Usaha :</strong> </td>
          <td width="38%">&nbsp;</td>
          <td width="21%">&nbsp;</td>
          <td width="16%">&nbsp;</td>
        </tr>
		<?php if($totalRows_gethasil > 0) { ?>
		<?php $tothasil = 0; $totbeban = 0; do { ?>
		<?php
		mysql_select_db($database_con_gl, $con_gl);
$query_getakhasil = "SELECT id, akun FROM gl_akun WHERE gl_akun.klasifikasi='$row_gethasil[kd]' ORDER BY gl_akun.id, gl_akun.akun";
$getakhasil = mysql_query($query_getakhasil, $con_gl) or die(mysql_error());
$row_getakhasil = mysql_fetch_assoc($getakhasil);
$totalRows_getakhasil = mysql_num_rows($getakhasil);
		?>
            <tr valign="top" bgcolor="#FFFFFF">
              <td align="right"><strong><?php echo $row_gethasil['klasifikasi']; ?></strong></td>
              <td align="left">&nbsp;</td>
              <td align="right">&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
			<?php do { ?>
			<?php
		mysql_select_db($database_con_gl, $con_gl);
if($_POST['Mulai']<>'Mulai...' AND $_POST['Sampai']<>'Sampai...') {
$query_sumhasil ="SELECT sum(gl_trans.total) FROM gl_trans, gl_rtrans where gl_rtrans.no_ref = gl_trans.no_ref AND gl_trans.akun='$row_getakhasil[id]' AND gl_rtrans.tgl between '$_POST[Mulai]' AND '$_POST[Sampai]' AND gl_trans.pos='D' GROUP by gl_trans.akun";
}else{
$query_sumhasil = "SELECT sum(gl_trans.total) FROM gl_trans, gl_rtrans WHERE  gl_rtrans.no_ref = gl_trans.no_ref AND gl_trans.akun='$row_getakhasil[id]' AND gl_trans.pos='D'";
}
if(!strstr($_SESSION[akses],'A-1-4')) {
	$query_sumhasil .= " AND gl_rtrans.gudang='$_SESSION[lokasi]'";
}
$sumhasil = mysql_query($query_sumhasil, $con_gl) or die(mysql_error());
$row_sumhasil = mysql_fetch_assoc($sumhasil);
$totalRows_sumhasil = mysql_num_rows($sumhasil);
mysql_select_db($database_con_gl, $con_gl);
if($_POST['Mulai']<>'Mulai...' AND $_POST['Sampai']<>'Sampai...') {
$query_sumhasil2 ="SELECT sum(gl_trans.total) FROM gl_trans, gl_rtrans where gl_rtrans.no_ref = gl_trans.no_ref AND gl_trans.akun='$row_getakhasil[id]' AND gl_rtrans.tgl between '$_POST[Mulai]' AND '$_POST[Sampai]' AND gl_trans.pos='K' GROUP by gl_trans.akun";
}else{
$query_sumhasil2 = "SELECT sum(gl_trans.total) FROM gl_trans, gl_rtrans WHERE gl_rtrans.no_ref = gl_trans.no_ref AND gl_trans.akun='$row_getakhasil[id]' AND gl_trans.pos='K'";
}
if(!strstr($_SESSION[akses],'A-1-4')) {
	$query_sumhasil2 .= " AND gl_rtrans.gudang='$_SESSION[lokasi]'";
}
$sumhasil2 = mysql_query($query_sumhasil2, $con_gl) or die(mysql_error());
$row_sumhasil2 = mysql_fetch_assoc($sumhasil2);
$totalRows_sumhasil = mysql_num_rows($sumhasil2);
$nil1 = intval($row_sumhasil['sum(gl_trans.total)']) - intval($row_sumhasil2['sum(gl_trans.total)']);
		?>
		<div id="box-exportdata"><a href="javascript:window.print();"><img src="images/_print.png" border="0" /></a></div>
          <tr valign="top">
          <td bgcolor="#FFFFFF">&nbsp;</td>
          <td align="left" bgcolor="#FFFFFF"><?php echo $row_getakhasil['id']; ?> - <?php echo $row_getakhasil['akun']; ?></td>
          <td align="right" bgcolor="#FFFFFF" style="border-bottom:dotted 1px #1C3554;"><?php if($nil1 >=0) { echo number_format($nil1,0,',','.').",-";}else{ echo "( ".number_format(str_replace("-","",$nil1),0,',','.').",-"." )";} $tothasil +=$nil1;?></td>
          <td align="right" bgcolor="#FFFFFF">&nbsp;</td>
        </tr>
		<?php } while ($row_getakhasil = mysql_fetch_assoc($getakhasil)); ?>
		<?php } while ($row_gethasil = mysql_fetch_assoc($gethasil)); ?>
		<?php } ?>
        <tr>
          <td bgcolor="#FFFFFF">&nbsp;</td>
          <td align="right" bgcolor="#FFFFFF">&nbsp;</td>
          <td align="right" bgcolor="#FFFFFF"><span style="font-weight: bold">Total :</span></td>
          <td align="right" bgcolor="#EEEEEE" style="border-bottom:dotted 1px #1C3554;"><?php if($tothasil >=0) { echo number_format($tothasil,0,',','.').",-";}else{ echo "( ".number_format(str_replace("-","",$tothasil),0,',','.').",-"." )";}?></td>
        </tr>
        <tr>
          <td align="left"><strong>Beban Usaha :</strong> </td>
          <td align="center">&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
	  <?php if($totalRows_getbeban > 0) { ?>
      <?php do { ?>
	  <?php
	  mysql_select_db($database_con_gl, $con_gl);
$query_getakbeban = "SELECT id, akun FROM gl_akun WHERE gl_akun.klasifikasi='$row_getbeban[kd]' ORDER BY gl_akun.id, gl_akun.akun";
$getakbeban = mysql_query($query_getakbeban, $con_gl) or die(mysql_error());
$row_getakbeban = mysql_fetch_assoc($getakbeban);
$totalRows_getakbeban = mysql_num_rows($getakbeban);
	  ?>
        <tr valign="top">
          <td align="right" bgcolor="#FFFFFF"><strong><?php echo $row_getbeban['klasifikasi']; ?></strong></td>
          <td align="left" bgcolor="#FFFFFF">&nbsp;</td>
          <td align="right" bgcolor="#FFFFFF">&nbsp;</td>
          <td bgcolor="#FFFFFF">&nbsp;</td>
        </tr>
		<?php do { ?>
		<?php
		mysql_select_db($database_con_gl, $con_gl);
if($_POST['Mulai']<>'Mulai...' AND $_POST['Sampai']<>'Sampai...') {
$query_sumbeban ="SELECT sum(gl_trans.total) FROM gl_trans, gl_rtrans where gl_rtrans.no_ref = gl_trans.no_ref AND gl_trans.akun='$row_getakbeban[id]' AND gl_rtrans.tgl between '$_POST[Mulai]' AND '$_POST[Sampai]'";
if($row_getbeban['tipe']==7) {
	$query_sumbeban .=" AND gl_trans.pos='K'";
}else{
	$query_sumbeban .=" AND gl_trans.pos='D'";
}
if(!strstr($_SESSION[akses],'A-1-4')) {
	$query_sumbeban .= " AND gl_rtrans.gudang='$_SESSION[lokasi]'";
}
$query_sumbeban .=" GROUP by gl_trans.akun";
}else{
$query_sumbeban = "SELECT sum(gl_trans.total) FROM gl_trans, gl_rtrans WHERE gl_rtrans.no_ref = gl_trans.no_ref AND gl_trans.akun='$row_getakbeban[id]'";
if($row_getbeban['tipe']==7) {
	$query_sumbeban .=" AND gl_trans.pos='K'";
}else{
	$query_sumbeban .=" AND gl_trans.pos='D'";
}
if(!strstr($_SESSION[akses],'A-1-4')) {
	$query_sumbeban .= " AND gl_rtrans.gudang='$_SESSION[lokasi]'";
}
}
$sumbeban = mysql_query($query_sumbeban, $con_gl) or die(mysql_error());
$row_sumbeban = mysql_fetch_assoc($sumbeban);
$totalRows_sumbeban = mysql_num_rows($sumbeban);
		?>
        <tr>
          <td bgcolor="#FFFFFF">&nbsp;</td>
          <td align="left" bgcolor="#FFFFFF"><?php echo $row_getakbeban['id']; ?> - <?php echo $row_getakbeban['akun']; ?></td>
          <td align="right" bgcolor="#FFFFFF" style="border-bottom:dotted 1px #1C3554;"><?php echo number_format($row_sumbeban['sum(gl_trans.total)'],0,',','.').",-";$totbeban +=$row_sumbeban['sum(gl_trans.total)'];?></td>
          <td align="right" bgcolor="#FFFFFF">&nbsp;</td>
        </tr>
		<?php } while ($row_getakbeban = mysql_fetch_assoc($getakbeban)); ?>
		<?php } while ($row_getbeban = mysql_fetch_assoc($getbeban)); ?>
		<?php } ?>
        <tr>
          <td bgcolor="#FFFFFF">&nbsp;</td>
          <td align="right" bgcolor="#FFFFFF">&nbsp;</td>
          <td align="right" bgcolor="#FFFFFF"><span style="font-weight: bold">Total :</span></td>
          <td align="right" bgcolor="#EEEEEE" style="border-bottom:dotted 1px #1C3554;"><?php echo number_format($totbeban,0,',','.').",-";?></td>
        </tr>
      <tr>
        <td bgcolor="#FFFFFF">&nbsp;</td>
        <td align="center" bgcolor="#FFFFFF">&nbsp;</td>
        <td align="right" bgcolor="#FFFFFF"><span style="font-weight: bold">Rugi / Laba Bersih (Net) :</span></td>
        <td align="right" bgcolor="#FFFFCC" style="border-bottom:dotted 1px #1C3554;"><?php $rlnet = str_replace("-","",$tothasil) - $totbeban; if($rlnet <=0) { echo number_format(str_replace("-","",$rlnet),0,',','.').",-";}else{ echo "( ".number_format(str_replace("-","",$rlnet),0,',','.').",-"." )";}?></td>
      </tr>
	  <?php if($totalRows_getpajak > 0) { ?>
      <?php  $totpaj = 0; do { ?>
        <?php $totpajak += $totpjk; } while ($row_getpajak = mysql_fetch_assoc($getpajak)); ?>
		<?php } ?>
		<?php if($totalRows_getbiaya > 0)  { ?>
        <?php $totbiaya = 0; do { ?>
          <?php $totbiaya += $potbiaya;} while ($row_getbiaya = mysql_fetch_assoc($getbiaya)); ?>
		  <?php } ?>
            </table></td>
  </tr>
   <?php }else if(isset($_POST['Search']) AND $totalRows_gethasil == 0 AND $totalRows_getbeban == 0){ ?>
  <tr>
    <td>Data tidak ada !!! </td>
  </tr><?php } ?>
</table>
